<?php
// load Wordpress
require_once('../../../wp-load.php');
require_once('../../../wp-admin/includes/admin.php');
do_action('admin_init');

// load AjaxTableEditor
require_once('mate/php/lang/LangVars-en.php');
require_once('mate/php/AjaxTableEditor.php');
require_once('reg_table.php');

class RegAjax
{
	function logError($message, $file, $line)
	{
		$message = sprintf($this->langVars->errInScript,$file,$line,$message);
		var_dump($message);
		die;
	}

	function initiateEditor()
	{
		global $current_user;
		get_currentuserinfo();
		$first_name = get_usermeta($current_user->ID,'first_name');
		$last_name = get_usermeta($current_user->ID,'last_name');
		$locality = get_usermeta($current_user->ID,'locality');
		$phone = get_usermeta($current_user->ID,'phone');
		$email = $current_user->user_email;

		$tableColumns['reg_type'] = array('display_text' => 'Reg. Type', 'perms' => 'EVCTAXQSHO', 'req' => true, 'select_array' => array('4 day full' => '4 day full-time', '4 day A' => '4 day part-time A', '4 day B' => '4 day part-time B', 'monthly' => 'monthly only'),
										  'col_header_info' => 'class="tooltip" title="Registration Type"', 
										  'input_info' => 'class="tooltip" title="Choose the registration type."' );
		$tableColumns['new'] = array('display_text' => 'New', 'perms' => 'EVCTAXQSHO', 'req' => true, 'display_mask' => "if(registrations6.new, 'Yes', 'No')", 'select_array' => array(0 => 'No', 1 => 'Yes'), 'default' => 1,
									 'col_header_info' => 'class="tooltip" title="New Trainee"',
									 'input_info' => 'class="tooltip" title="Indicate if trainee is new (Yes) or is continuing (No)"' );		$tableColumns['id'] = array('display_text' => 'ID', 'perms' => 'VQSXO'); 
		$tableColumns['first_name'] = array('display_text' => 'First', 'perms' => 'EVCTAXQSHO', 'req' => true, 'default' => $first_name,
											'col_header_info' => 'class="tooltip" title="First Name"', 
											'input_info' => 'class="tooltip" title="Defaults to your name. Change for other registraitons"' );
		$tableColumns['last_name'] = array('display_text' => 'Last', 'perms' => 'EVCTAXQSHO', 'req' => true, 'default' => $last_name,
										   'col_header_info' => 'class="tooltip" title="Last Name"', 
										   'input_info' => 'class="tooltip" title="Defaults to your name. Change for other registrations"' );
		$tableColumns['age'] = array('display_text' => 'Age', 'perms' => 'EVCTAXQSHO', 'req' => true, 'display_mask' => "CASE registrations6.age WHEN 25 THEN '1-25' WHEN 35 THEN '26-35' WHEN 45 THEN '36-45' WHEN 46 THEN '46-99' END", 'select_array' => array(25 => '< 25', 35 => '26 - 35', 45 => '36 - 45', 46 => '> 46'),
									 'col_header_info' => 'class="tooltip" title="Age Range"', 
									 'input_info' => 'class="tooltip" title="Select the age range of the registrant."');
		$tableColumns['bro_sis'] = array('display_text' => 'Bro/Sis', 'perms' => 'EVCTAXQSHO', 'req' => true, 'select_array' => array('B' => 'Brother', 'S' => 'Sister'),
										 'col_header_info' => 'class="tooltip" title="Brother or Sister"', 
									 'input_info' => 'class="tooltip" title="Brother or sister."');
		$tableColumns['email'] = array('display_text' => 'Email', 'perms' => 'EVCTAXQSHO', 'default' => $email,
									   'col_header_info' => 'class="tooltip" title="Email"', 
									   'input_info' => 'class="tooltip" title="Defaults to your email. Change for other registrations. Not required."' );
		$tableColumns['phone'] = array('display_text' => 'Phone', 'perms' => 'EVCTAXQSHO', 'default' => $phone,
									   'col_header_info' => 'class="tooltip" title="Phone"', 
									   'input_info' => 'class="tooltip" title="Defaults to your phone. Change for other registrations. Not required."' );
		$tableColumns['locality'] = array('display_text' => 'Locality', 'perms' => 'EVCTAXQSHO', 'req' => true, 'join' => array('table' => 'localities', 'column' => 'id', 'display_mask' => 'localities.city', 'type' => 'left'), 'default' => $locality,
										  'col_header_info' => 'class="tooltip" title="Locality"', 
										  'input_info' => 'class="tooltip" title="Select the registrant\'s locality. Choose \'Other\' if not listed."' );
//		$tableColumns['5day_room'] = array('display_text' => '5-day Room', 'perms' => 'EVCTAXQSHO', 'select_array' => array('basic' => 'Basic Accomodations', 'moderate' => 'Moderate Accomodations', 'deluxe' => 'Deluxe Accomodations'),
//										   'col_header_info' => 'class="tooltip" title="Room Type for 5-day"', 
//										   'input_info' => 'class="tooltip" title="If registering for the 5-day training, choose the room type."' ); 
		$tableColumns['registrator'] = array('display_text' => 'Registrator', 'perms' => 'VCAXQ', 'join' => array('table' => 'wp_users', 'column' => 'ID', 'display_mask' => 'wp_users.display_name', 'type' => 'left'), 'default' => $current_user->ID, 'hidden_add' => true, 'hidden_edit' => true,
											 'col_header_info' => 'class="tooltip" title="User who made this registration."', 
											 'input_info' => 'class="tooltip" title="User who made this registration."' );
		$tableColumns['reg_time'] = array('display_text' => 'Reg. Time', 'perms' => 'VQSXO',
										  'col_header_info' => 'class="tooltip" title="Time the registration was made."',
										  'input_info' => 'class="tooltip" title="Time this registration was made."');
		$tableColumns['language'] = array('display_text' => 'Language', 'perms' => 'EVCTAXQSHO', 'req' => true, 'select_array' => array('English' => 'English', 'Chinese' => 'Chinese', 'Spanish' => 'Spanish', 'Korean' => 'Korean'), 'default' => 'English',
										  'col_header_info' => 'class="tooltip" title="Meeting language"',
										  'input_info' => 'class="tooltip" title="Select the language of the training meetings the registrant would like to attend."');
		$tableColumns['binder_lang'] = array('display_text' => 'Binder', 'perms' => 'EVCTAXQSHO', 'req' => true, 'select_array' => array('English' => 'English', 'Chinese' => 'Chinese', 'Spanish' => 'Spanish', 'Korean' => 'Korean'), 'default' => 'English',
										  'col_header_info' => 'class="tooltip" title="Binder Language"',
										  'input_info' => 'class="tooltip" title="Select the language of the primary GOW binder the registrant would like."');
		$tableColumns['binder2_lang'] = array('display_text' => 'Binder2', 'perms' => 'EVCTAXQSHO', 'req' => false, 'select_array' => array('None'=>'None', 'English' => 'English', 'Chinese' => 'Chinese', 'Spanish' => 'Spanish', 'Korean' => 'Korean'), 'default' => 'None',
										  'col_header_info' => 'class="tooltip" title="Binder Language"',
										  'input_info' => 'class="tooltip" title="Select the language of the secondary GOW binder the registrant would like."');		$tableColumns['district'] = array('display_text' => 'District', 'perms' => 'EVCTAXQSHO', 'req' => true, 'join' => array('table' => 'districts', 'column' => 'id', 'display_mask' => 'districts.name', 'type' => 'left'),
										  'col_header_info' => 'class="tooltip" title="District"', 
										  'input_info' => 'class="tooltip" title="Select the registrant\'s district."' );
//		$tableColumns['roomate_req'] = array('display_text' => 'Roomate', 'perms' => 'EVCAXQSHO', 
//									   'col_header_info' => 'class="tooltip" title="Roomate Request"', 
//									   'input_info' => 'class="tooltip" title="Request for certain roomates during the 5-day training. Not required."' );
		
		$tableName = 'registrations6';
		$primaryCol = 'id';
		$errorFun = array(&$this,'logError');
		$permissions = 'UEAVMDQCSXHO';

		$this->Editor = new FixAjaxTableEditor($tableName,$primaryCol,$errorFun,$permissions,$tableColumns);
		$this->Editor->setConfig('varPrefix','adminView4');
		$this->Editor->setConfig('tableTitle', 'Training Administrator View');
		$this->Editor->setConfig('tableInfo','cellpadding="1" class="mateTable"');
		$this->Editor->setConfig('orderByColumn','locality');
		$this->Editor->setConfig('addRowTitle','Add Registration');
		$this->Editor->setConfig('editRowTitle','Edit Registration');
	}

	function RegAjax()
	{
		if ( ! is_user_logged_in() ) {
			$this->logError('User needs to be logged in',__FILE__, __LINE__);
		}
		if ( !current_user_can('edit_published_posts') ) {
			$this->logError('Reg error: user does not have read permissions',__FILE__, __LINE__);
		}
		if(isset($_POST['json']))
		{
			session_start();
			// Initiating lang vars here is only necessary for the logError, and mysqlConnect functions in Common.php.
			// If you are not using Common.php or you are using your own functions you can remove the following line of code.
			$this->langVars = new LangVars();

			if(ini_get('magic_quotes_gpc'))
			{
				$_POST['json'] = stripslashes($_POST['json']);
			}
			if(function_exists('json_decode'))
			{
				$data = json_decode($_POST['json']);
			}
			else
			{
				require_once('/mate/php/JSON.php');
				$js = new Services_JSON();
				$data = $js->decode($_POST['json']);
			}
			if(empty($data->info) && strlen(trim($data->info)) == 0)
			{
				$data->info = '';
			}
			$this->initiateEditor();
			$this->Editor->main($data->action,$data->info);
			if(function_exists('json_encode'))
			{
				echo json_encode($this->Editor->retArr);
			}
			else
			{
				echo $js->encode($this->Editor->retArr);
			}
		}
		else if(isset($_GET['export']))
		{
			session_start();
			ob_start();
			$this->initiateEditor();
			echo $this->Editor->exportInfo();
			header("Cache-Control: no-cache, must-revalidate");
			header("Pragma: no-cache");
			header("Content-type: application/x-msexcel");
			header('Content-Type: text/csv');
			header('Content-Disposition: attachment; filename="'.$this->Editor->tableName.'.csv"');
			exit();
		}
	}
}

$regAjax = new RegAjax();